package com.music.controller;

import com.github.pagehelper.PageInfo;
import com.music.pojo.Album;
import com.music.pojo.Audio;
import com.music.pojo.SingleSong;
import com.music.pojo.UserInfo;
import com.music.service.SingleSongService;
import com.music.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("singleSong")
public class SingleSongController {
    @Autowired
    private SingleSongService singleSongService;

    Audio audio;
    @RequestMapping("search")
    @ResponseBody
    public PageInfo<SingleSong> songPageInfo( String search, Integer pageNum) throws UnsupportedEncodingException {
        String _search = URLDecoder.decode(search,"UTF-8");
        return singleSongService.findSingleSongsBySearch(_search,pageNum);
    }

    @RequestMapping(value = "{mid}",method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo findByMid(@PathVariable String mid,HttpSession session){
        ResultInfo resultInfo = new ResultInfo(true);
        SingleSong singleSong = singleSongService.findWithCommentsAndSingerByMid(mid);
        audio = new Audio();
        audio.setName(singleSong.getMname());
        audio.setArtist(singleSong.getSinger().getSname());
        audio.setLrc(singleSong.getLyc());
        audio.setCover("/music_platform"+singleSong.getPic());
        audio.setUrl("/music_platform"+singleSong.getSource());
        session.setAttribute("audio",audio);
        resultInfo.setData(singleSong);
        return resultInfo;
    }

    //是否收藏歌曲
    @RequestMapping("isFavorite")
    @ResponseBody
    public ResultInfo isFavorite(Integer mid, HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo;
        if (userInfo==null){
            return new ResultInfo(false);
        }
        Integer num = singleSongService.isFavorite(mid,userInfo.getUid());
        if (num==0){
            resultInfo = new ResultInfo(false);
        }else {
            resultInfo = new ResultInfo(true);
        }
        resultInfo.setData(num);
        return resultInfo;
    }

    @RequestMapping("getAudio")
    @ResponseBody
    public ResultInfo getAudio(HttpSession session){
//        Audio audio = (Audio) session.getAttribute("audio");
        ResultInfo resultInfo = new ResultInfo(true);
        resultInfo.setData(audio);
        return resultInfo;
    }

    @RequestMapping("songsNumCount")
    @ResponseBody
    public ResultInfo SongsNumCount(Integer sid){
        Integer num = singleSongService.findSongsNumCount(sid);
        ResultInfo resultInfo = new ResultInfo(true);
        resultInfo.setData(num);
        return resultInfo;
    }

    @RequestMapping("soaring")
    @ResponseBody
    public PageInfo<SingleSong> findSoaringSongs(Integer pageNum){
        return singleSongService.findSoaringSongs(pageNum);
    }
    @RequestMapping("new")
    @ResponseBody
    public PageInfo<SingleSong> findNewSongs(Integer pageNum){
        return singleSongService.findNewSongs(pageNum);
    }
    @RequestMapping("hot")
    @ResponseBody
    public PageInfo<SingleSong> findHotSongs(Integer pageNum){
        return singleSongService.findHotSong(pageNum);
    }

    //查找关注页面的最近新歌曲
    @RequestMapping("findUserFollowSingerSongs")
    @ResponseBody
    public ResultInfo findUserFollowSingerSongs(HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo;
        if (userInfo==null){
            resultInfo = new ResultInfo(false);
        }else{
            resultInfo = new ResultInfo(true);
            List<SingleSong> list = singleSongService.findUserFollowSingerSongs(userInfo.getUid());
            resultInfo.setData(list);
        }
        return resultInfo;
    }
}
